<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    :width="!props.isReadonly ? '850px' : '850px'"
    draggable
    @close="closeDialog"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="120"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="异常信息编号" prop="qualityExceptionCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.qualityProblemCode"
              placeholder="请输入异常信息编号"
              :disabled="true"
              clearable
            />

            <span v-else>{{ formData.qualityProblemCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="问题分类" prop="problemTypeCode">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.problemTypeCode"
              placeholder="请选择问题分类"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('ExceptionType')"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
            <span v-else>{{ formData.problemTypeCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产线/过程" prop="lineProcessName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lineProcessName"
              placeholder="请输入生产线/过程"
              clearable
            />

            <span v-else>{{ formData.lineProcessName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="失效模式" prop="failureMode">
            <el-tree-select
              v-if="!props.isReadonly"
              placeholder="请选择失效模式"
              v-model="formData.failureMode"
              :data="failureModeList"
              node-key="id"
              :props="{
                label: 'failureModeContent',
                value: 'id',
                children: 'nodes',
              }"
              :render-after-expand="false"
              :check-strictly="true"
              style="width: 300px"
              clearable
              @node-click="handleNodeClick"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="型号/编号" prop="materialProcessId">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.materialProcessId"
              placeholder="请选择型号/编号"
              clearable
              @change="changeFigureNumber"
            >
              <el-option
                v-for="item in materialProcessList"
                :label="`${item.materialProcessFigure}`"
                :value="`${item.id}`"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 点击型号带出产品 -->
          <el-form-item label="产品/项目名" prop="productProjectName">
            <el-input
              v-if="!props.isReadonly"
              disabled
              v-model="formData.productProjectName"
              placeholder="请输入产品/项目名"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品过程状态" prop="productProcess">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.productProcess"
              placeholder="请选择产品过程状态"
              clearable
              style="width: 100%"
              @change="handleChangeMaterialProcessState"
            >
              <el-option
                v-for="item in materialProcessStateList"
                :key="item.id"
                :label="`${item.materialProcessStateCode}-${item.materialProcessStateContent}`"
                :value="String(item.id)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="失效后果及影响" prop="consequenceInfluence">
            <el-input
              v-if="!props.isReadonly"
              maxlength="255"
              show-word-limit
              :autosize="{ minRows: 1, maxRows: 2 }"
              type="textarea"
              v-model="formData.consequenceInfluence"
              placeholder="请输入失效后果及影响"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否内部制造" prop="isInternal">
            <el-radio-group
              v-model="formData.isInternal"
              v-if="!props.isReadonly"
              @change="changIsInternal"
            >
              <el-radio :label="1">内部生产</el-radio>
              <el-radio :label="0">外部生产</el-radio>
            </el-radio-group>
            <span v-else>{{ formData.isInternal }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="formData.isInternal == 0">
          <el-form-item
            label="生产单位"
            :prop="formData.isInternal == 0 ? 'supplyName' : ''"
          >
            <!-- <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.supplyId"
              placeholder="请选择生产单位"
              clearable
              @change="changSupplyList"
            >
              <el-option
                v-for="item in supplyList"
                :label="`${item.partnerCode}-${item.partnerName}`"
                :value="`${item.id}`"
              />
            </el-select>
            <span v-else>{{ formData.supplyId }}</span> -->
            <el-input
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.supplyName"
              placeholder="请选择生产单位"
              clearable
            />
            <span v-else>{{ formData.supplyName }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="发生地点" prop="occurrenceLocation">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.occurrenceLocation"
              placeholder="请输入发生地点"
              clearable
            />
            <span v-else>{{ formData.occurrenceLocation }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="问题发生频度" prop="problemFrequency">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.problemFrequency"
              placeholder="请输入问题发生频度"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="涉及数量" prop="qualityQuantity">
            <el-input-number
              v-if="!props.isReadonly"
              v-model="formData.qualityQuantity"
              style="width: 100%"
              :min="1"
              :precision="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任部门" prop="departmentId">
            <el-tree-select
              v-if="!props.isReadonly"
              placeholder="请选择责任部门"
              v-model="formData.departmentId"
              :data="organizationList"
              :render-after-expand="false"
              :check-strictly="true"
              node-key="id"
              :props="{
                label: 'organizationCode',
                value: 'id',
                children: 'nodes',
              }"
              style="width: 100%"
              @node-click="handleChangeDepartment"
              clearable
            />
            <span v-else>{{ formData.departmentId }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="目标设定" prop="targetTime">
            <el-date-picker
              v-model="formData.targetTime"
              type="date"
              placeholder="请输入目标设定"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-descriptions
      v-show="props.isReadonly"
      class="margin-top"
      title=""
      :column="2"
      size="large"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">异常信息编号</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.qualityProblemCode || "" }}
        </div>
      </el-descriptions-item>

      <el-descriptions-item>
        <template #label>
          <div class="cell-item">生产线/过程</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.lineProcessName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">失效模式</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.failureModeContent || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">型号/编号</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.sortCode || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">产品/项目名</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.productProjectName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">产品过程状态</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.productProcessContent }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">失效后及影响</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.consequenceInfluence || "" }}
        </div>
      </el-descriptions-item>

      <!-- <el-descriptions-item>
        <template #label>
          <div class="cell-item">信息来源</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.informationSourceContent || "" }}
        </div>
      </el-descriptions-item> -->

      <el-descriptions-item :span="2">
        <template #label>
          <div class="cell-item">发生地点</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.occurrenceLocation || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">问题发生频度</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.problemFrequency || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">涉及数量</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.qualityQuantity ? formData.qualityQuantity : "0" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">责任部门</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.departmentName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">目标设定</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{
            formData.targetTime
              ? dayjs(formData.targetTime).format("YYYY-MM-DD")
              : ""
          }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">是否内部制造</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.isInternal == 1 ? "内部生产" : "外部生产" || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item v-if="formData.isInternal == 0">
        <template #label>
          <div class="cell-item">生产单位</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.supplyName }}
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import dayjs from "dayjs";
// import html2pdf from "./PDF.js";
const dictionaryStore = useDictStoreHook();

function PDFprit() {
  const table = document.getElementById("table");
  // html2pdf(table, {
  //   background: "#fff",
  //   minimumUnit: "tr",
  //   orientation: "l",
  // });
  // html2pdf("table", "对账单", dayjs(new Date()).format("YYYY-mm-dd"));
}
defineOptions({
  name: "Q8DQUALICHECKLISTEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  qualityExceptionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],

  productionTypeId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productionTypeName: [
    { required: true, message: "请选择产品类目名称", trigger: "change" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  figureNumber: [{ required: true, message: "请选择图号", trigger: "change" }],
  qualityQuantity: [
    // { required: true, message: "请输入数量", trigger: "blur" },
  ],
  failureQuantity: [
    // { required: true, message: "请输入失效对应数量", trigger: "blur" },
  ],
  failureMode: [
    { required: true, message: "请选择失效模式", trigger: "change" },
  ],
  occurrenceTime: [
    { required: true, message: "请选择发生时间", trigger: "change" },
  ],
  occurrenceLocation: [
    { required: true, message: "请选择发生地点", trigger: "change" },
  ],
  informationSourceId: [
    // { required: true, message: "请选择信息来源", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  informationSourceContent: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  exceptionState: [],
  isInternal: [],
  supplyId: [
    { required: true, message: "请选择生产单位", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  problemTypeCode: [
    { required: true, message: "请选择问题分类", trigger: "change" },
  ],
  lineProcessName: [
    { required: true, message: "请输入生产线/过程", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productProcess: [
    { required: true, message: "请选择产品/过程状态", trigger: "change" },
  ],
  problemFrequency: [
    { required: true, message: "请输入问题发生频度", trigger: "blur" },
  ],
  //   lineProcessName: [
  //     // { required: true, message: "请选择责任部门", trigger: "change" },
  //   ],

  departmentId: [
    { required: true, message: "请输入责任部门", trigger: "blur" },
  ],
  targetTime: [{ required: true, message: "请输入目标设定", trigger: "blur" }],
  materialProcessId: [
    { required: true, message: "请选择型号编号", trigger: "change" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  qualityProblemCode: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  informationSourceId: "",
  lineProcessName: "",
  productProjectName: "",
  sortCodeId: "",
  sortCode: "",
  productProcess: "",
  failureMode: "",
  consequenceInfluence: "",
  problemFrequency: "",
  proposeDepartmentId: "",
  proposeDepartmentCode: "",
  proposeDepartmentName: "",
  proposeManagerId: "",
  proposeManagerName: "",
  departmentId: "",
  departmentCode: "",
  departmentName: "",
  departmentManagerId: "",
  departmentManagerName: "",
  isInternal: 1,
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  problemTime: "",
  problemUserId: "",
  problemUserName: "",
  problemTypeCode: "",
  exceptionTypeSubId: "",
  exceptionTypeSubCode: "",
  exceptionTypeSubName: "",
  exceptionLevel: "",
  problemApproachId: "",
  problemApproachCode: "",
  problemApproachType: "",
  problemApproachName: "",
  releaseState: "",
  auditUserId: "",
  auditUserName: "",
  provisionalMeasuresDeadline: "",
  rectificationDeadline: "",
  rectificationRequirement: "",
  agreeWith: "",
  approvalOpinion: "",
  approvalTime: "",
  targetTime: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  // 涉及数量
  qualityQuantity: 1,
  // 失效模式
  failureModeContent: "",
  // 发生地点
  occurrenceLocation: "",
  // 状态
  productProcessContent: "",
  sortName: "",
  // 图号编号
  materialProcessCode: "",
  materialProcessName: "",
  materialProcessId: "",
  materialProcessFigure: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevel,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        // 图号编号
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
      } = newVal;
      Object.assign(formData, {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevel,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
      });
      console.log("formData", formData);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.qualityProblemCode = "";
  formData.qualityExceptionId = "";
  formData.qualityExceptionCode = "";
  formData.informationSourceId = "";
  formData.lineProcessName = "";
  formData.productProjectName = "";
  formData.sortCodeId = "";
  formData.sortCode = "";
  formData.productProcess = "";
  formData.failureMode = "";
  formData.consequenceInfluence = "";
  formData.problemFrequency = "";
  formData.proposeDepartmentId = "";
  formData.proposeDepartmentCode = "";
  formData.proposeDepartmentName = "";
  formData.proposeManagerId = "";
  formData.proposeManagerName = "";
  formData.departmentId = "";
  formData.departmentCode = "";
  formData.departmentName = "";
  formData.departmentManagerId = "";
  formData.departmentManagerName = "";
  formData.isInternal = 1;
  formData.supplyId = "";
  formData.supplyCode = "";
  formData.supplyName = "";
  formData.problemTime = "";
  formData.problemUserId = "";
  formData.problemUserName = "";
  formData.problemTypeCode = "";
  formData.exceptionTypeSubId = "";
  formData.exceptionTypeSubCode = "";
  formData.exceptionTypeSubName = "";
  formData.exceptionLevel = "";
  formData.problemApproachId = "";
  formData.problemApproachCode = "";
  formData.problemApproachType = "";
  formData.problemApproachName = "";
  formData.releaseState = "";
  formData.auditUserId = "";
  formData.auditUserName = "";
  formData.provisionalMeasuresDeadline = "";
  formData.rectificationDeadline = "";
  formData.rectificationRequirement = "";
  formData.agreeWith = "";
  formData.approvalOpinion = "";
  formData.approvalTime = "";
  formData.targetTime = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.qualityQuantity = 1;
  formData.occurrenceLocation = "";
  formData.productProcessContent = "";
  formData.sortName = "";
  formData.materialProcessCode = "";
  formData.materialProcessName = "";
  formData.materialProcessId = "";
  formData.materialProcessFigure = "";
}

const loading = ref(false);

/** 质量异常信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/qms/q8dqualityproblem/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 产品过程状态列表
const materialProcessStateList = ref([]);
const getMaterialProcessStateList = async () => {
  let res = await request({
    url: "/qms/qmsmaterialprocessstate/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  console.log("产品状态", res.data);

  materialProcessStateList.value = res.data;
};

// 改变产品状态
const handleChangeMaterialProcessState = (e) => {
  console.log(e);

  if (e) {
    materialProcessStateList.value.forEach((item) => {
      if (e == item.id) {
        formData.productProcessContent = item.materialProcessStateContent;
      }
    });
    console.log("产品状态", formData.productProcessContent);
  } else {
    formData.productProcessContent = "";
  }
};

// 供应商列表
const supplyList = ref([]);
async function getSupplyList() {
  let res = await request({
    url: "mdm/mdmpartner/getListByFull",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  supplyList.value = res.data;
}

const changSupplyList = (e) => {
  if (e) {
    supplyList.value.forEach((item) => {
      if (item.id == e) {
        formData.supplyName = item.partnerName;
        formData.supplyCode = item.partnerCode;
      }
    });
  } else {
    formData.supplyName = "";
    formData.supplyCode = "";
  }
};

const changIsInternal = (e) => {
  console.log(e);
  formData.supplyId = "";
  formData.supplyName = "";
  formData.supplyCode = "";
};

// 责任部门

const organizationList = ref([]);
const getOrganizationList = async () => {
  let res = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 4 },
  }).finally(() => {
    loading.value = false;
  });
  organizationList.value = res.data;
  console.log("organizationList", organizationList.value);
};

const handleChangeDepartment = (e) => {
  if (e) {
    formData.departmentName = e.organizationName;
    formData.departmentCode = e.organizationCode;
  } else {
    formData.departmentName = "";
    formData.departmentCode = "";
  }
};

// 失效模式树列表
const failureModeList = ref([]);
async function getFailureMode() {
  let res = await request({
    url: "/qms/qmsfailuremode/getTree",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  failureModeList.value = res.data;
  console.log("failureMode", failureModeList.value);
}

const handleNodeClick = (e) => {
  if (e) {
    formData.failureModeContent = e.failureModeContent;
  } else {
    formData.failureModeContent = "";
  }
};

// 图号编号列表
const materialProcessList = ref([]);
async function getMaterialProcess() {
  let res = await request({
    url: "/qms/qmsmaterialprocess/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  materialProcessList.value = res.data;
  console.log("materialProcessList", materialProcessList.value);
}
const changeFigureNumber = (e) => {
  if (e) {
    materialProcessList.value.forEach((item) => {
      if (item.id == e) {
        formData.materialProcessCode = item.materialProcessCode;
        formData.materialProcessName = item.materialProcessName;
        formData.materialProcessFigure = item.materialProcessFigure;
        formData.productProjectName = item.materialProcessName;
      }
    });
  } else {
    formData.materialProcessCode = "";
    formData.materialProcessName = "";
    formData.materialProcessFigure = "";
    formData.productProjectName = "";
  }
};

onMounted(() => {
  getFailureMode();
  getMaterialProcessStateList();
  getOrganizationList();
  // getSupplyList();
  getMaterialProcess();
});
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
